<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class CataLog extends Model
{
    protected $guarded = [];

    protected $appends = ['purchased'];

    public function getPurchasedAttribute()
    {
        if (Auth::id()) {
            if($this->userCourses()
                ->where('user_id', Auth::id())
                ->where('pay_status', 1)
                ->count()) {
                return true;
            }
        }
        return false;
    }

    public function userCourses()
    {
        return $this->morphMany(UserCourse::class, 'courseable');
    }

    public function course()
    {
        return $this->belongsTo(Course::class);
    }
}
